package com.woniuxy.microsystemsarserver.configure;


import com.woniuxy.microsystemsarserver.handle.StringToDateConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @Configuration 该类是一个配置类
 */

@Component
@Configuration
public class SpringMVCConfigure implements WebMvcConfigurer {

    /**
     * 跨域支持
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")//允许所有的请求
                .allowCredentials(true).//允许做身份认证
                allowedMethods("GET","POST","PUT","DELETE");//允许支持的跨域方法有哪些
    }
    /**
     * 配置类型转换器
     * @param registry
     */
    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverter(stringToDateConverter());
    }
    /**
     * @Bean 用于将组件的实例，装配到Spring容器中
     * 方法的方法名：stringToDateConverter 作为组件在容器的ID
     * 默认：作用域为：单例
     * @return
     */
    @Bean
    public StringToDateConverter stringToDateConverter(){
        return new StringToDateConverter();
    }





}

